<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>引入canvas</title>
  </head>
  <body>
    <button>下载</button>
    <script>
      const canvas = document.createElement("canvas");
      canvas.width = 400;
      canvas.height = 400;
      document.body.append(canvas);

      const ctx = canvas.getContext("2d");
      ctx.fillRect(100, 100, 200, 200);
      const btn = document.querySelector("button");
      btn.onclick = function () {
        const url = canvas.toDataURL();
        const a = document.createElement("a");
        a.href = url;
        a.download = "canvas画布";
        a.click;
      };
    </script>
  </body>
</html>
